Dart Set operator ==
Syntax & Examples
Set.operator == operator
The `==` operator in Dart is used to test for equality between two objects.
Syntax of Set.operator ==
The syntax of Set.operator == operator is:
operator ==(dynamic other) → boolThis operator == operator of Set the equality operator.
Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
other | required | The object to compare for equality with the current object. |
✐ Examples
1 Check equality of identical sets
In this example,
- We create two sets
set1andset2with the same elements in the same order. - We use the
==operator to compare if the sets are equal. - We print the result to standard output.
Dart Program
void main() {
Set<int> set1 = {1, 2, 3};
Set<int> set2 = {1, 2, 3};
bool result = set1 == set2;
print('Are sets equal? $result');
}Output
Are sets equal? true
2 Check equality of different sets
In this example,
- We create two sets
set1andset2with the same elements but in different order. - We use the
==operator to compare if the sets are equal. - We print the result to standard output.
Dart Program
void main() {
Set<int> set1 = {1, 2, 3};
Set<int> set2 = {1, 3, 2};
bool result = set1 == set2;
print('Are sets equal? $result');
}Output
Are sets equal? false
Summary
In this Dart tutorial, we learned about operator == operator of Set: the syntax and few working examples with output and detailed explanation for each example.